<script>
init({
  title: 'Issue #3345',
  desc: 'Check/Uncheck All in all page with server side(<a href="https://github.com/wenzhixin/bootstrap-table/issues/1167" target="_blank">#3345</a>).',
  links: ['bootstrap-table.min.css'],
  scripts: ['bootstrap-table.min.js']
})
</script>

<template>
  <div class="toolbar">
    <button
      id="checkAll"
      class="btn btn-secondary"
    >
      Check All
    </button>
    <button
      id="uncheckAll"
      class="btn btn-secondary"
    >
      Uncheck All
    </button>
  </div>
  <table
    id="table"
    data-toggle="table"
    data-url="https://examples.wenzhixin.net.cn/examples/bootstrap_table/data"
    data-pagination="true"
    data-side-pagination="server"
    data-page-list="[5, 10, 20, 50, 100, 200]"
    data-search="true"
    data-toolbar=".toolbar"
    data-response-handler="responseHandler"
  >
    <thead>
      <tr>
        <th
          data-field="state"
          data-checkbox="true"
        ></th>
        <th data-field="id">
          ID
        </th>
        <th data-field="name">
          Item Name
        </th>
        <th data-field="price">
          Item Price
        </th>
      </tr>
    </thead>
  </table>
</template>

<script>
  let checkAll = false

  function mounted () {
    $('#checkAll').click(function () {
      checkAll = true
      $('#table').bootstrapTable('checkAll')
    })
    $('#uncheckAll').click(function () {
      checkAll = false
      $('#table').bootstrapTable('uncheckAll')
    })
  }

  window.responseHandler = res => {
    res.rows.forEach(function (item) {
      item.state = checkAll
    })
    return res
  }
</script>
